package com.single.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.config.entity.Equipment;
import com.config.entity.LineConfig;
import com.config.service.EquipmentService;
import com.config.service.LineConfigService;
import com.single.service.SingleService;


@RequestMapping("/single")
@Controller
public class SingleController {
	@Resource(name="lineConfigService")
	private LineConfigService lineService;
	@Resource(name="equipmentService")
	private EquipmentService equipmentService;
	@Resource(name="singleService")
	private SingleService singleService;
	
	/**
	 * 单机综合能耗分析
	 * @return
	 */
	@RequestMapping("/comprehensive/{type}")
	public String comprehensiveAnalysis(Model model,@PathVariable(value = "type") String type){
		model.addAttribute("currentName","单机综合能耗分析");
		if(type.equals("day")){//日综合
			return "single/comprehensive/comprehensiveday";
		}else if(type.equals("complex")){
			return "single/comprehensive/comprehensivecomplex";
		}else if(type.equals("week")){
			return "single/comprehensive/comprehensiveweek";
		}else{
			return "single/comprehensive/comprehensivemonth";
		}
	}
	/**
	 * 单机功率分析
	 * @return
	 */
	@RequestMapping("/power/{type}")
	public String powerAnalysis(Model model,@PathVariable(value = "type") String type){
		model.addAttribute("currentName","单机功率分析");
		if(type.equals("complex")){//日综合
			return "single/power/powercomplex";
		}else if(type.equals("week")){//周分析
			return "single/power/powerweek";
		}else if(type.equals("month")){
			return "single/power/powermonth";
		}else{
			return "single/power/powerday";
		}
	}
	/**
	 * 单机电度电费分析
	 * @return
	 */
	@RequestMapping("/electricity/{type}")
	public String electricityAnalysis(Model model,@PathVariable(value = "type") String type){
		model.addAttribute("currentName","单机电度电费分析");
		if(type.equals("day")){//日
			return "single/electricity/electricityday";
		}else if(type.equals("complex")){//日综合
			return "single/electricity/electricitycomplex";
		}else if(type.equals("week")){//周
			return "single/electricity/electricityweek";
		}else{
			return "single/electricity/electricitymonth";
		}
	}
	/**
	 * 单机开动率
	 * */
	@RequestMapping("/startrate/{type}")
	public String startrateAnalysis(Model model,@PathVariable(value = "type") String type){
		model.addAttribute("currentName","单机开动率分析");
		if(type.equals("day")){//日
			return "single/startrate/startrateday";
		}else if(type.equals("complex")){//日综合
			return "single/startrate/startratecomplex";
		}else if(type.equals("week")){//周
			return "single/startrate/startrateweek";
		}else{
			return "single/startrate/startratemonth";
		}
	}


	/**
	 * 单机日查询
	 * @param type：  区分功率、电度电费、开动率
	 * @param lineId  生产线id
	 * @param equId   设备id
	 * @param belongDate   查询日期，默认当前日期(YYYY-MM-DD)
	 * @return
	 */
	@RequestMapping("/singleDay/{type}")
	public Map<String, Object> singleDay(@PathVariable(value = "type") String type,@RequestParam(value="lineId",required=false,defaultValue="0")int lineId,
			@RequestParam(value="equId",required=false,defaultValue="0")int equId,String belongDate){
		Map<String, Object> model=new HashMap<String, Object>();
		if(lineId==0&&equId==0){
			//获取第一条生产线
			LineConfig line=lineService.findFirstLine();
			if(line!=null){
				//获取第一台设备
				Equipment equ=equipmentService.listByLine(line.getId()).get(0);
				//默认刷新第一条生产线 第一台设备
				if(type.equals("power")){
					singleService.getSingleDayData(line.getId(), equ.getId(), belongDate, model);
				}else if(type.equals("electricity")){
					singleService.getElectricityDayData(line.getId(), equ.getId(), belongDate, model);
				}else if(type.equals("startrate")){
					singleService.getStartrateDayData(line.getId(), equ.getId(), belongDate, model);
				}else if(type.equals("comprehensive")){
					singleService.getComprehensiveDayData(line.getId(), equ.getId(), belongDate, model);
				}
			}
		}else{
			//根据前台所选的生产线和设备刷新
			if(type.equals("power")){
				singleService.getSingleDayData(lineId, equId, belongDate, model);
			}else if(type.equals("electricity")){
				singleService.getElectricityDayData(lineId, equId, belongDate, model);
			}else if(type.equals("startrate")){
				singleService.getStartrateDayData(lineId, equId, belongDate, model);
			}else if(type.equals("comprehensive")){
				singleService.getComprehensiveDayData(lineId, equId, belongDate, model);
			}
		}
		return model;
	}
	
	/**
	 * 单机日综合查询
	 * @param type     区分功率、电度电费、开动率
	 * @param lineId   生产线id
	 * @param equId    设备id
	 * @param startTime   开始日期(YYYY-MM-DD)
	 * @param endTime     结束日期(YYYY-MM-DD)
	 * @return
	 */
	@RequestMapping("/singleComplex/{type}")
	public Map<String, Object> singleComplex(@PathVariable(value = "type") String type,@RequestParam(value="lineId",required=false,defaultValue="0")int lineId,
			@RequestParam(value="equId",required=false,defaultValue="0")int equId,String startTime,String endTime){
		Map<String, Object> model=new HashMap<String, Object>();
		if(lineId==0&&equId==0){
			//获取第一条生产线
			LineConfig line=lineService.findFirstLine();
			if(line!=null){
				//获取第一台设备
				Equipment equ=equipmentService.listByLine(line.getId()).get(0);
				//默认刷新第一条生产线 第一台设备
				if(type.equals("power")){
					singleService.getSinglePowerCompexData(line.getId(), equ.getId(), startTime,endTime, model);
				}else if(type.equals("electricity")){
					singleService.getElectricityCompexData(line.getId(), equ.getId(), startTime, endTime, model);
				}else if(type.equals("startrate")){
					singleService.getStartrateCompexData(line.getId(), equ.getId(), startTime, endTime, model);
				}else if(type.equals("comprehensive")){
					singleService.getComprehensiveCompexData(line.getId(), equ.getId(), startTime, endTime, model);
				}
			}
		}else{
			//根据前台所选的生产线和设备刷新
			if(type.equals("power")){
				singleService.getSinglePowerCompexData(lineId, equId, startTime,endTime,model);
			}else if(type.equals("electricity")){
				singleService.getElectricityCompexData(lineId, equId,startTime, endTime, model);
			}else if(type.equals("startrate")){
				singleService.getStartrateCompexData(lineId, equId, startTime, endTime, model);
			}else if(type.equals("comprehensive")){
				singleService.getComprehensiveCompexData(lineId, equId, startTime, endTime, model);
			}
		}
		return model;
	}
	
	/**
	 * 单机功率分析--周分析/月分析
	 * @param type     区分 周分析，月分析
	 * @param lineId   生产线id
	 * @param equId    设备id
	 * @param year     所属年份
	 * @param startTime      开始时间(周/月)
	 * @param endTime        结束时间(周/月)
	 * @return
	 */
	@RequestMapping("/singlePower/{type}")
	public Map<String, Object> singlePowerWeekMonthOrWeek(@PathVariable(value = "type") String type,
			@RequestParam(value="lineId",required=false,defaultValue="0")Integer lineId,
			@RequestParam(value="equId",required=false,defaultValue="0")Integer equId,Integer year,Integer startTime,Integer endTime){
		Map<String, Object> model=new HashMap<String, Object>();
		 String stateType="超载";
		if(lineId==0&&equId==0){
			//获取第一条生产线
			LineConfig line=lineService.findFirstLine();
			if(line!=null){
				//获取第一台设备
				Equipment equ=equipmentService.listByLine(line.getId()).get(0);
				//默认刷新第一条生产线 第一台设备
				if(type.equals("week")){
					singleService.getSinglePowerWeekData(equ.getId(), stateType, year, startTime, endTime, model);
				}else{
					singleService.getSinglePowerMonthData(equ.getId(), stateType, year, startTime, endTime, model);
				}
			}
		}else{
			//根据前台所选的生产线和设备刷新
			if(type.equals("week")){
				singleService.getSinglePowerWeekData(equId, stateType, year, startTime, endTime, model);
			}else{
				singleService.getSinglePowerMonthData(equId, stateType, year, startTime, endTime, model);
			}
		}
		return model;
	}
	
	
	/**
	 * 单机电度电费--周分析/月分析
	 * @param type     区分 周分析，月分析
	 * @param lineId   生产线id
	 * @param equId    设备id
	 * @param year     所属年份
	 * @param startTime      开始时间(周/月)
	 * @param endTime        结束时间(周/月)
	 * @return
	 */
	@RequestMapping("/singlelElectricity/{type}")
	public Map<String, Object> singlelElectricityMonthOrWeek(@PathVariable(value = "type") String type,@RequestParam(value="lineId",required=false,defaultValue="0")int lineId,
			@RequestParam(value="equId",required=false,defaultValue="0")int equId,int year,int startTime,int endTime){
		Map<String, Object> model=new HashMap<String, Object>();
		if(lineId==0&&equId==0){
			//获取第一条生产线
			LineConfig line=lineService.findFirstLine();
			if(line!=null){
				//获取第一台设备
				Equipment equ=equipmentService.listByLine(line.getId()).get(0);
				//默认刷新第一条生产线 第一台设备
				if(type.equals("month")){
					singleService.getElectricityMonthData(equ.getId(), year, startTime, endTime, model);
				}else{
					singleService.getElectricityWeekData(equ.getId(), year, startTime, endTime, model);
				}
			}
		}else{
			//根据前台所选的生产线和设备刷新
			if(type.equals("month")){
				singleService.getElectricityMonthData(equId, year, startTime, endTime, model);
			}else{
				
				singleService.getElectricityWeekData(equId, year, startTime, endTime, model);
			}
		}
		return model;
	}
	
	

	/**
	 * 单机开动率--周分析/月分析
	 * @param type     区分 周分析，月分析
	 * @param lineId   生产线id
	 * @param equId    设备id
	 * @param year     所属年份
	 * @param startTime      开始时间(周/月)
	 * @param endTime        结束时间(周/月)
	 * @return
	 */
	@RequestMapping("/singlelStartrate/{type}")
	public Map<String, Object> singlelStartrateMonthOrWeek(@PathVariable(value = "type") String type,@RequestParam(value="lineId",required=false,defaultValue="0")int lineId,
			@RequestParam(value="equId",required=false,defaultValue="0")int equId,int year,int startTime,int endTime){
		Map<String, Object> model=new HashMap<String, Object>();
		if(lineId==0&&equId==0){
			//获取第一条生产线
			LineConfig line=lineService.findFirstLine();
			if(line!=null){
				//获取第一台设备
				Equipment equ=equipmentService.listByLine(line.getId()).get(0);
				//默认刷新第一条生产线 第一台设备
				if(type.equals("month")){
					singleService.getStartrateMonthData(equ.getId(), year, startTime, endTime, model);
				}else{
					singleService.getStartrateWeekData(equ.getId(), year, startTime, endTime, model);
				}
			}
		}else{
			//根据前台所选的生产线和设备刷新
			if(type.equals("month")){
				singleService.getStartrateMonthData(equId, year, startTime, endTime, model);
			}else{
				
				singleService.getStartrateWeekData(equId, year, startTime, endTime, model);
			}
		}
		return model;
	}
	
	
	
	/**
	 * 单机月同比
	 * @param type  区分 功率、电度电费，开动率
	 * @param equId  设备id
	 * @param month  所属月份
	 * @return
	 */
	@RequestMapping("/singleBySameMonth/{type}")
	public Map<String, Object> singleBySameMonth(@PathVariable(value = "type") String type,
			@RequestParam(value="equId",required=false,defaultValue="0")int equId,int month){
		Map<String, Object> model=new HashMap<String, Object>();
		 String stateType="超载";
		if(equId==0){
			//获取第一条生产线
			LineConfig line=lineService.findFirstLine();
			if(line!=null){
				Equipment equ=equipmentService.listByLine(line.getId()).get(0);
				if(type.equals("powerMonth")){
					singleService.getSinglePowerMonthDataBySameMonth(equ.getId(), stateType, month, model);
				}else if(type.equals("electricityMonth")){
					singleService.getElectricityMonthDataBySameMonth(equ.getId(), month, model);
				}else{
					singleService.getStartrateMonthDataBySameMonth(equ.getId(), month, model);
				}
			}
		}else{
			if(type.equals("powerMonth")){
				singleService.getSinglePowerMonthDataBySameMonth(equId, stateType, month, model);
			}else if(type.equals("electricityMonth")){
				singleService.getElectricityMonthDataBySameMonth(equId, month, model);
			}else{
				singleService.getStartrateMonthDataBySameMonth(equId, month, model);
			}
		}
		return model;
	}
	
	/**
	 * 单机综合能耗--周分析/月分析
	 * @param type     区分 周分析，月分析
	 * @param lineId   生产线id
	 * @param equId    设备id
	 * @param year     所属年份
	 * @param startTime      开始时间(周/月)
	 * @param endTime        结束时间(周/月)
	 * @return
	 */
	@RequestMapping("/singlelComprehensive/{type}")
	public Map<String, Object> singlelComprehensiveMonthOrWeek(@PathVariable(value = "type") String type,@RequestParam(value="lineId",required=false,defaultValue="0")int lineId,
			@RequestParam(value="equId",required=false,defaultValue="0")int equId,int year,int startTime,int endTime){
		Map<String, Object> model=new HashMap<String, Object>();
		if(lineId==0&&equId==0){
			//获取第一条生产线
			LineConfig line=lineService.findFirstLine();
			if(line!=null){
				//获取第一台设备
				Equipment equ=equipmentService.listByLine(line.getId()).get(0);
				//默认刷新第一条生产线 第一台设备
				if(type.equals("month")){
					singleService.getComprehensiveMonthData(equ.getId(), year, startTime, endTime, model);
				}else{
					singleService.getComprehensiveWeekData(equ.getId(), year, startTime, endTime, model);
				}
			}
		}else{
			//根据前台所选的生产线和设备刷新
			if(type.equals("month")){
				singleService.getComprehensiveMonthData(equId, year, startTime, endTime, model);
			}else{
				singleService.getComprehensiveWeekData(equId, year, startTime, endTime, model);
			}
		}
		return model;
	}
}
